package com.corpgovernment.organization.event;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.corpgovernment.api.organization.model.org.request.GetOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.request.SaveOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.response.GetOrgConfigResponse;
import com.corpgovernment.organization.dataloader.BasicDataClientLoader;
import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.corpgovernment.organization.enumm.OrgTypeEnum;
import com.corpgovernment.organization.service.CommonService;
import com.corpgovernment.organization.service.IOrgConfigService;
import com.corpgovernment.organization.service.IOrgPayTypeService;
import com.corpgovernment.organization.service.ImportOrganizationConfigService;
import com.ctrip.corp.obt.generic.core.event.EventSubscriber;
import com.ctrip.corp.obt.generic.core.event.EventType;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;

import lombok.extern.slf4j.Slf4j;

/**
 *
 */
@Slf4j
@Component
public class OrgInitSubscriber implements EventSubscriber<OrgInitEvent> {

    @Autowired
    private CommonService commonService;
    @Autowired
    private IOrgPayTypeService orgPayTypeService;
    @Autowired
    private BasicDataClientLoader basicDataClientLoader;
    @Autowired
    private IOrgConfigService orgConfigService;
    @Autowired
    private ImportOrganizationConfigService importOrganizationConfigService;

    @Override
    public void onEvent(OrgInitEvent event) {
        log.info("接收创建初始操作：[{}]", JsonUtils.toJsonString(event));
        if (Objects.nonNull(event.getInitOrgPayType())){
            log.info("接收创建初始操作：[{}]", JsonUtils.toJsonString(event));
            //初始化组织支付方式
            initPayType(event);
            // 初始化组织设置
            initOrganization(event.getInitOrgPayType().getOrgId(), event.getOrgConfigList());
            //初始化公司消息通知配置
            initMsgConfig(event.getInitOrgPayType());
        }

    }
    @Override
    public EventType eventType() {
        return EventType.LOCAL;
    }

    private void initMsgConfig(OrgInitEvent.InitOrgPayType initOrgPayType) {
        if (!Objects.equals(OrgTypeEnum.D.getCode(), initOrgPayType.getOrgType())) {
            basicDataClientLoader.InitMsgSendConfig(initOrgPayType.getOrgId());
        }
    }


    /**
     * 初始化组织支付方式
     */
    public void initPayType(OrgInitEvent event){
        OrgInitEvent.InitOrgPayType initOrgPayType = event.getInitOrgPayType();
        //删除关联支付信息
        orgPayTypeService.delete(Collections.singletonList(initOrgPayType.getOrgId()), null, null, null, true);
        commonService.savePayType(initOrgPayType.getOrgId(), initOrgPayType.getOrgType(),event.getPayTypeConfigList());
    }
    /**
     * 有配置的，用配置的
     * 否则用父级的
     * 没有父级的，用默认的
     */
    public void initOrganization(String orgId, List<OrgInitEvent.OrgConfigType> orgConfigList) {
        // 有配置的，用配置的
        if (CollectionUtils.isNotEmpty(orgConfigList)){
            importOrganizationConfigService.importOrganizationConfig(orgConfigList);
            return;

        }
        MbOrgInfo mbOrgInfo = commonService.getFirstCompanyInfo(orgId);
        if (mbOrgInfo == null){
            return;
        }
        GetOrgConfigRequest getOrgConfigRequest = buildOrgConfigRequest(mbOrgInfo);
        GetOrgConfigResponse response = orgConfigService.getOrgConfig(getOrgConfigRequest);
        SaveOrgConfigRequest request = buildSaveOrgConfigRequest(orgId, response);
        orgConfigService.saveOrgConfig(request);
    }

    private  SaveOrgConfigRequest buildSaveOrgConfigRequest(String orgId, GetOrgConfigResponse response) {
        SaveOrgConfigRequest request = new SaveOrgConfigRequest();
        request.setOrgId(orgId);
        request.setOrgConfigInfo(response.getOrgConfigInfo());
        return request;
    }

    private  GetOrgConfigRequest buildOrgConfigRequest(MbOrgInfo mbOrgInfo) {
        GetOrgConfigRequest getOrgConfigRequest = new GetOrgConfigRequest();
        getOrgConfigRequest.setOrgId(mbOrgInfo.getOrgId());
        return getOrgConfigRequest;
    }


}
